package com.onlocation.objects;

import android.database.Cursor;
import com.onlocation.util.PreferenceUtil;
import com.onlocation.util.StringUtil;
import com.onlocation.util.VolumeUtil;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: Steve Carrell
 * Date: 5/10/12
 */
public class Location {
    private String name;
    private Double longitude;
    private Double latitude;
    private Float radius;
    private Integer volume;
    private Boolean vibrate;

    public Location() {
        name = "";
        longitude = 0.0;
        latitude = 0.0;
        radius = 0.0f;
        volume = VolumeUtil.getCurrentVolume();
        vibrate = VolumeUtil.getCurrentIsVibrate();
    }

    public Location(Cursor c) {
        name = c.getString(0);
        longitude = c.getDouble(1);
        latitude = c.getDouble(2);
        radius = c.getFloat(3);
        volume = c.getInt(4);
        vibrate = c.getInt(5) > 0;
    }

    public boolean equals(Location location) {
        return StringUtil.equalsIgnoreNull(name, location.name) &&
                longitude.equals(location.longitude) &&
                latitude.equals(location.latitude) &&
                radiusToPrefMeas(radius).equals(radiusToPrefMeas(location.radius)) &&
                volume.equals(location.volume) &&
                vibrate.equals(location.vibrate);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setRadius(Float radius) {
        this.radius = radius;
    }

    public Float getRadius() {
        return radius;
    }
    
    public void setVolume(Integer volume) {
        this.volume = volume;
    }
    
    public Integer getVolume() {
        return volume;
    }
    
    public void setVibrate(Boolean vibrate) {
        this.vibrate = vibrate;
    }

    public Boolean getVibrate() {
        return vibrate;
    }
    
    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<String, Object>(6);
        map.put("name", name);
        map.put("longitude", longitude);
        map.put("latitude", latitude);
        map.put("radius", radius);
        map.put("volume", volume);
        map.put("vibrate", vibrate);
        return map;
    }

    public static Float radiusToMeters(float radius) {
        float ratio = PreferenceUtil.getRadiusUnitsMultiplier();
        // Use DecimalFormat to round our value to 4 decimal places
        String s = new DecimalFormat("#0.0###").format(radius * ratio);
        return Float.valueOf(s);
    }

    public static Integer radiusToPrefMeas(float radius) {
        float ratio = PreferenceUtil.getRadiusUnitsMultiplier();
        // Should always be an integer value since round has an int return type.
        int prefMeas = Math.round(radius / ratio);
        // Only allowing 4 digits, so reduce to 9999 if necessary.
        return (prefMeas > 9999) ? 9999 : prefMeas;
    }
}
